// Author: Simon-Pierre Boucher // Contact: contact@spboucher.ai // Project: chat.spboucher.ai import { NextResponse, type NextRequest } from "next/server"; import { requireSession } from "@/lib/auth/guard"; import { deleteConversation, getConversation, listMessages, getMessage, updateConversation, } from "@/lib/conversations"; export const runtime = "nodejs"; type Params = { params: Promise<{ id: string }> }; export async function GET(_req: NextRequest, { params }: Params) { const { unauthorized } = await requireSession(); if (unauthorized) return unauthorized; const { id } = await params; const conversation = getConversation(id); if (!conversation) return NextResponse.json({ error: "Conversation not found." }, { status: 404 }); return NextResponse.json({ conversation, messages: listMessages(id) }); } export async function PATCH(req: NextRequest, { params }: Params) { const { unauthorized } = await requireSession(); if (unauthorized) return unauthorized; const { id } = await params; if (!getConversation(id)) return NextResponse.json({ error: "Conversation not found." }, { status: 404 }); let body: { title?: string; pinned?: boolean; currentLeafId?: string | null }; try { body = await req.json(); } catch { return NextResponse.json({ error: "Invalid request." }, { status: 400 }); } if (body.currentLeafId) { const leaf = getMessage(body.currentLeafId); if (!leaf || leaf.conversation_id !== id) { return NextResponse.json({ error: "Invalid leaf message." }, { status: 400 }); } } updateConversation(id, body); return NextResponse.json({ conversation: getConversation(id) }); } export async function DELETE(_req: NextRequest, { params }: Params) { const { unauthorized } = await requireSession(); if (unauthorized) return unauthorized; const { id } = await params; deleteConversation(id); return NextResponse.json({ ok: true }); }